package com.zxe.interceptor;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import com.zxe.constant.Constant;
import com.zxe.utils.HttpContextUtils;
import lombok.SneakyThrows;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;

/**
 * @Des 需要每年割接数据的拦截器
 * @Author ljf
 * @Date 2023/6/15 0015 11:14
 */
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class ProjectYearInterceptor implements InnerInterceptor {


    private final String projectTableName = " region_apply_project_info_zdxm ";
    private final String projectLabel = " project_for_label ";
    private final String projectStatus = " project_status ";
    private final String taskTableName = "TASK_DECOMPOSE_GOAL_SETTING";
    private final String threeTableName = "three_year_move_scheduling_task";


    private final String underline = "_";



    @SneakyThrows
    @Override
    public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) {
        String thisYear = getRequestYear();
        String taskYear = getRequestTaskYear();
        String threeYear = getRequestThreeYear();
        MetaObject metaObject = SystemMetaObject.forObject(sh);
        String sql = (String) metaObject.getValue("delegate.boundSql.sql")+" ";
        sql = sql.replaceAll("AS\\s*'([A-Z]+)'", "as '$1'.toLowerCase()");
        sql = sql.replaceAll("\\bas\\b", "AS");
        sql = sql.replaceAll("AS\\s*'([^']*)'", "AS \"$1\"");
        if(StrUtil.isNotBlank(thisYear) && !(DateUtil.thisYear()+"").equals(thisYear)){
            // 这里可以根据业务逻辑修改sql语句，例如替换表名
            sql = sql.replaceAll("(?i)"+projectTableName, " "+projectTableName.trim()+underline+thisYear+" ");
            /*sql = sql.replaceAll("(?i)"+projectLabel, " "+projectLabel.trim()+underline+thisYear+" ");
            sql = sql.replaceAll("(?i)"+projectStatus, " "+projectStatus.trim()+underline+thisYear+" ");*/
            metaObject.setValue("delegate.boundSql.sql", sql);
        }
        // 三年行动任务分解
        else if(StrUtil.isNotBlank(taskYear) && !(DateUtil.thisYear()+"").equals(taskYear)){
            // 这里可以根据业务逻辑修改sql语句，例如替换表名
            sql = sql.replaceAll("(?i)"+taskTableName, " "+taskTableName.trim()+underline+taskYear+" ");
            metaObject.setValue("delegate.boundSql.sql", sql);
        }
        // 三年行动任务调度 (暂时没用)
        else if(StrUtil.isNotBlank(threeYear) && !(DateUtil.thisYear()+"").equals(threeYear)){
            // 这里可以根据业务逻辑修改sql语句，例如替换表名
            sql = sql.replaceAll("(?i)"+threeTableName, " "+threeTableName.trim()+underline+threeYear+" ");
            metaObject.setValue("delegate.boundSql.sql", sql);
        }
    }

    /**
     * 获取请求的Year
     */
    public static String getRequestYear(){
        HttpServletRequest httpRequest = HttpContextUtils.getHttpServletRequest();
        if(ObjectUtil.isNotEmpty(httpRequest)){
            //从header中获取Year
            String year = httpRequest.getHeader(Constant.YEAR);

            //如果header中不存在Year，则从参数中获取Year
            if(StrUtil.isBlank(year)){
                year = httpRequest.getParameter(Constant.YEAR);
            }

            return year;
        }
        return null;
    }

    /**
     * 获取请求的taskYear(三年行动分解任务)
     */
    private String getRequestTaskYear(){
        HttpServletRequest httpRequest = HttpContextUtils.getHttpServletRequest();
        if(ObjectUtil.isNotEmpty(httpRequest)){
            //从header中获取Year
            String year = httpRequest.getHeader(Constant.TASK_YEAR);

            //如果header中不存在Year，则从参数中获取Year
            if(StrUtil.isBlank(year)){
                year = httpRequest.getParameter(Constant.TASK_YEAR);
            }

            return year;
        }
        return null;
    }

    /**
     * 获取请求的threeYear(三年行动任务调度)
     */
    private String getRequestThreeYear(){
        HttpServletRequest httpRequest = HttpContextUtils.getHttpServletRequest();
        if(ObjectUtil.isNotEmpty(httpRequest)){
            //从header中获取Year
            String year = httpRequest.getHeader(Constant.THREE_YEAR);

            //如果header中不存在Year，则从参数中获取Year
            if(StrUtil.isBlank(year)){
                year = httpRequest.getParameter(Constant.THREE_YEAR);
            }

            return year;
        }
        return null;
    }

}